
#include "main.h"

monitor::monitor(QObject *parent) :
    QThread(parent)
{
    leak_water_timer=new QTimer();
    QObject:: connect( leak_water_timer, SIGNAL(timeout()), SLOT(leak_water_timeDone()) );
    leak_water_timer->setSingleShot(true);

  Vacuum_pump_timer=new QTimer();
  QObject:: connect( Vacuum_pump_timer, SIGNAL(timeout()), SLOT(Vacuum_pump_timeout()) );
  Vacuum_pump_timer->setSingleShot(true);

  if(whole_machine_parameter.is_water_supply_type)
  {
  water_supply_timer=new QTimer();
  QObject:: connect( water_supply_timer, SIGNAL(timeout()), SLOT(water_supply_timeout()) );
  water_supply_timer->start(10000);
  }

  water_valve_open_timer=new QTimer();
  QObject:: connect( water_valve_open_timer, SIGNAL(timeout()), SLOT(water_valve_open_timeout()) );
  water_valve_open_timer->setSingleShot(true);

   delay_heat_timer=new QTimer();
  QObject:: connect(  delay_heat_timer, SIGNAL(timeout()), SLOT(heat_slot()) );
   delay_heat_timer->setSingleShot(true);

  count=0;count2=0;
    SPK=false;

}

void monitor::stop()
{
    delete leak_water_timer;
    delete water_valve_open_timer;
    delete Vacuum_pump_timer;

}
void monitor::run()
{

    while(1)//QThread: Destroyed while thread is still running 结束时来不及退出
   {
       if(!test_mode) read_IO_stats();
       qApp->processEvents();
       msleep(250);
       qApp->processEvents();

      heating();
      //overpressure_protection();
      if(water_level_failure ) water=normal;//|| sterilizing
      else water_level();

      qApp->processEvents();
      msleep(250);
      qApp->processEvents();

      Vacuum_pump();
      workable_func();
      timing();

      if(whole_machine_parameter.machine_type_id>2)
      if(whole_machine_parameter.machine_type_id%2) //门未关到位打开排气阀
      {
      if(!front_door_closed_in_place||!back_door_closed_in_place)  exhaust_valve_ON
      }
      else
      {
          if(!front_door_closed_in_place)  exhaust_valve_ON
      }

   }
}


 void monitor::timing()
{
    if(light)  {LED1_OFF;light=0;}
    else   {LED1_ON;light=1;}

    if(SPK) {
        if(b)  {SPK_OFF LEDA_OFF b=0;}
        else   {SPK_ON LEDA_ON b=1;}
            }

}



 void monitor::heating()
{

    if(inter_pressure_switch_isreached)
   {
     heating_valve_OFF;
     if( sterilizing)
      {
         if(controller->pressure_balanced && !test_mode)
         {
             breakdown= PRESSURE_REACHED;
             STOP_CMD
             WARN->show_sound("故障：升温过程压力到位");
         }
     }
     else STOP_AUTO_HEAT

   }
    else
      {
         if( auto_heating && !heating_valve_isopen)    heating_valve_ON
      }

}

 void monitor::heat_slot()
 {
    set_pin_value(heating_valve_pin,heat_valve,heating_valve_isopen);
 }

 void monitor::delay_heat(bool b)
 {

    if(  heat_valve=b )
  {
     if(!delay_heat_timer->isActive())//heating_valve_isopen!=heat_valve &&
    {
     //QTimer::singleShot(900, this, SLOT(heat_slot()));
     delay_heat_timer->start(2000);
    }
  }
 else {
        heat_slot();
       if(delay_heat_timer->isActive()) delay_heat_timer->stop();
        }

 }

 void monitor::overpressure_protection()
{

    if(overpressure_protection_switch_isreached)
    {
        breakdown=OVERPRESSURE;
        heating_valve_OFF;
        STOP_CMD

        if( WARN->isHidden())
       {
       WARN->show_sound("夹层超压！");
        }

     }

}

 void monitor::water_level()
{
    static bool leak_water_warn_repeat;

    if(bottom_water_level_isreached)
  {
    if(top_water_level_isreached)
   {
        if(++count2==10)
        {
            count2=0;
            water= high;
            water_valve_OFF;
         }
    }
    else
    {
        water= normal;
        if( leak_water_timer->isActive()) leak_water_timer->stop();
        if(count2>0) count2--;
     }
       if(count>0)  count--;
       leak_water_warn_repeat=false;
       if(sterilizing) water_valve_OFF
   }
    else if( ++count==60)
    {
         count=0;
        if(water !=leak) {water= low; }//if(!sterilizing) water_valve_ON
        if( sterilization_stage < 3) water_valve_ON//怀疑两个超时事件同时到位导致程序崩溃
         heating_valve_OFF
        if(!leak_water_warn_repeat && !leak_water_timer->isActive())
         {
             leak_water_warn_repeat=true;
             leak_water_timer->start(300000);
         }
    }
}

 void monitor::water_valve_open_timeout()
 {
     //if(water_valve_isopen)
     if(water < high || (sterilizing && water < normal))
     {
     water_valve_OFF
     WARN->show_sound("缺水！水压不足或夹层压力过大或管道堵塞或阀门未打开");
     }
 }


 void monitor::leak_water_timeDone()
 {

       if(!bottom_water_level_isreached)
       {
         water=leak;
         workable=0;
         heating_valve_OFF
         STOP_CMD
         STOP_ACTION
         breakdown=LOWEST_WATER;
         water_valve_open_timeout();
        }
 }

 void monitor::Vacuum_pump_leak_cooling_water()
 {
    Vacuum_pump_cooling_water_timer_isrunning=false;
    if(!water_switch_isopen)
     {
         Vacuum_pump_workable=0;
         Vacuum_pump_OFF
         STOP_CMD
         breakdown=Vacuum_pump_NO_WATER;
         if(WARN->isHidden())
          {
               WARN->show_sound("没有冷却水流，真空泵不能使用！");
           }
     }
 }



void monitor::start_Vacuum_pump_timer()
{
    Vacuum_pump_timer->start(600000);
}

void monitor::stop_Vacuum_pump_timer()
{
    if(Vacuum_pump_timer->isActive())   Vacuum_pump_timer->stop();
}


 void monitor::Vacuum_pump_timeout()
{
        if(Vacuum_pump_isopen)
       {
        breakdown=Vacuum_pump_TimeOut;
        Vacuum_pump_OFF
        STOP_CMD
        WARN->show_sound("真空泵工作超时！");
        }
}




 void monitor::Vacuum_pump()
{
    if(Vacuum_pump_isopen) //真空泵先抽2S后没有冷却水再停止
    {
         if(!water_switch_isopen &&
                 !Vacuum_pump_cooling_water_timer_isrunning)
       {
        QTimer::singleShot(2000, this, SLOT(Vacuum_pump_leak_cooling_water()));
        Vacuum_pump_cooling_water_timer_isrunning=true;

       }
     }
    else    if(water_switch_isopen) { Vacuum_pump_workable=1;}
}

 void monitor::workable_func()
{

    if( water!=leak && door_closed_OK )   workable=true;//&& Vacuum_pump_workable
    else  { workable=false;  }

}


void monitor::water_supply_timeout()
{
    if( whole_machine_parameter.is_water_supply_type)
    {
         if(water< high)  water_valve_ON
    }
}

void monitor::read_IO_stats()
{

    bottom_water_level_isreached              = READ_GPIO_PIN(GPQDAT,5);//GPQ5//下水位
    top_water_level_isreached                 = READ_GPIO_PIN(GPQDAT,6);//GPQ6//上水位
    if(water_level_failure) water_switch_isopen=true;
    else water_switch_isopen                       =READ_GPIO_PIN(GPCDAT,0);//GPC0//水流开关
    inter_pressure_switch_isreached           = READ_GPIO_PIN(GPCDAT,1);//GPC1//夹层压力开关

    front_door_closed_in_place                =	READ_GPIO_PIN(GPCDAT,2);//GPC2
    front_door_opened_in_place                =	READ_GPIO_PIN(GPCDAT,3);//GPC3
    front_door_Sealing_strip_pressure_in_place= READ_GPIO_PIN(GPNDAT,6);// GPN6
    back_door_opened_in_place       	      = READ_GPIO_PIN(GPNDAT,9);//GPN9
    back_door_closed_in_place                 = READ_GPIO_PIN(GPNDAT,11);//GPN11
    back_door_Sealing_strip_pressure_in_place = READ_GPIO_PIN(GPLDAT,8);// GPL8
    overpressure_protection_switch_isreached  = READ_GPIO_PIN(GPLDAT,9);//GPL9 //超压保护开关
}


 bool monitor::read_pin(volatile uint *GPIO_Port,int pin)
{
    return READ_GPIO_PIN(GPIO_Port,pin);
}

 bool monitor::read_IO_status(int num)
{

     switch(num)
     {
     case 1: read_pin(GPEDAT,1);break;
     case 2: read_pin(GPEDAT,2);break;
     case 3: read_pin(GPEDAT,3);break;
     case 4: read_pin(GPEDAT,4);break;
     case 5: read_pin(GPMDAT,0);break;
     case 6: read_pin(GPMDAT,1);break;
     case 7: read_pin(GPMDAT,2);break;
     case 8: read_pin(GPMDAT,3);break;
     case 9: read_pin(GPMDAT,4);break;
     case 10: read_pin(GPMDAT,5);break;
     case 11: read_pin(GPQDAT,1);break;
     case 12: read_pin(GPQDAT,2);break;
     case 13: read_pin(GPQDAT,3);break;
     case 14: read_pin(GPQDAT,4);break;
     }

}

void monitor::set_pin(volatile uint *GPIO_Port,int pin,bool bvalue)
{
    if(bvalue) SET_GPIO_ON(GPIO_Port,pin)
    else SET_GPIO_OFF(GPIO_Port,pin)

}

void monitor::set_output_pin(int num,bool bvalue)
{

    switch(num)
    {
    case 1: set_pin(GPEDAT,1,bvalue);break;
    case 2: set_pin(GPEDAT,2,bvalue);break;
    case 3: set_pin(GPEDAT,3,bvalue);break;
    case 4: set_pin(GPEDAT,4,bvalue);break;
    case 5: set_pin(GPMDAT,0,bvalue);break;
    case 6: set_pin(GPMDAT,1,bvalue);break;
    case 7: set_pin(GPMDAT,2,bvalue);break;
    case 8: set_pin(GPMDAT,3,bvalue);break;
    case 9: set_pin(GPMDAT,4,bvalue);break;
    case 10: set_pin(GPMDAT,5,bvalue);break;
    case 11: set_pin(GPQDAT,1,bvalue);break;
    case 12: set_pin(GPQDAT,2,bvalue);break;
    case 13: set_pin(GPQDAT,3,bvalue);break;
    case 14: set_pin(GPQDAT,4,bvalue);break;
    }
    COM_CMD(4*(num-1),bvalue)
}
  void monitor::set_pin_value(int pin,bool value,bool &IOstatus)
  {
    if( value != IOstatus)
    {
        IOstatus=value;
        set_output_pin(pin,value);
    }
  }

